<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight webCMS
 * Copyright (C) 2005 Leo Feyer
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at http://www.gnu.org/licenses/.
 *
 * PHP version 5
 * @copyright  Psitrax Websolutions 
 * @author     Christoph Wiechert <wio@psitrax.de> 
 * @package    CeFlash 
 * @license    LGPL 
 * @filesource
 */

class CeFlash extends ContentText {
	
	
	public function generate(){
		if(TL_MODE == 'BE') {
			$tpl = 'be_wildcard';
			$this->Template = new BackendTemplate($tpl);
			$this->Template->wildcard = '### FLASH Object ###  ('.$this->ce_flash_movie.')';
			return $this->Template->parse();
		} else {
			if (strlen($this->arrData['space'][0]))	{
				$this->arrStyle[] = 'margin-top:'.$this->arrData['space'][0].'px;';
			}
			if (strlen($this->arrData['space'][1])) 	{
				$this->arrStyle[] = 'margin-bottom:'.$this->arrData['space'][1].'px;';
			}
			unset($this->arrData['space']); // ContentText souldnt use it again
			$outTpl = new FrontendTemplate('ce_flash');
			$outTpl->cssID = strlen($this->cssID[0]) ? ' id="' . $this->cssID[0] . '"' : '';
			$outTpl->class = trim('ce_flash ' . $this->cssID[1]);
			$outTpl->style = count($this->arrStyle) ? implode(' ', $this->arrStyle) : '';
			$this->cssID = ""; // ContentText souldnt use it again
			$this->arrStyle = array(); // ContentText souldnt use it again 
			$outTpl->code = '<div id="FO'.$this->id.'" class="block ufo swfobject">'.parent::generate().'</div>';
			return $outTpl->parse();

		}
	}
	
	
	protected function compile(){	
		
		// Flash-Content
		
		$size = unserialize($this->ce_flash_size);
		// $version = unserialize($this->ce_flash_version);
		
		$params = unserialize($this->ce_flash_params);
		if(!is_array($params)) $params = array();
		$paramStr = '';
		foreach($params as $par) {
			if(trim($par[0])=='') continue;
			$paramStr .= ", {$par[0]}:'{$par[1]}'";
		}
		$paramStr = substr($paramStr,2);
		
		$vars = unserialize($this->ce_flash_vars);
		if(!is_array($vars)) $vars = array();
		$varStr = '';
		foreach($vars as $var) {
			if(trim($var[0])=='') continue;
			$varStr .= ", {$var[0]}:'{$var[1]}'";
		}
		$varStr = substr($varStr,2);
		
		// Javascript-Code
		$GLOBALS['TL_HEAD'][] = '
		<script type="text/javascript">
		window.addEvent( "domready", function() {'.
		(($GLOBALS['TL_CONFIG']['unobtrusiveFlash'] === true) ? 'if(Browser.Plugins.Flash.version >= '.$GLOBALS['TL_CONFIG']['flashVersion'].') {' : '')
		.'   var FO'.$this->id.' = new Swiff("'.$this->ce_flash_movie.'", {
				container: $("FO'.$this->id.'"), 
				width: '.$size[0].',
				height: '.$size[1].',
				// version: "'.$version[0].'.'.$version[1].'",
				vars: {'.$varStr.'},
				params: {'.$paramStr.'}
		    });'.
		  (($GLOBALS['TL_CONFIG']['unobtrusiveFlash'] === true) ? '}' : '')
	        .'});
		</script>'; 
			
		// Let ContentText compile the alt-text
		$this->text = $this->ce_flash_text;
		parent::compile();

	}
	
}

?>